﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TankBattle.Models
{
    public class AutoTank: Tank
    {
        #region Variable

        protected int count;
        protected Random random;

        #endregion

        #region Properties

        public bool AllowShoot
        {
            get
            {
                if (timeCount >= shootDelay)
                {
                    timeCount = 0;
                    return true;
                }
                return false;
            }
        }

        #endregion 

        #region Construction
        public AutoTank()
        { }
        public AutoTank(Texture2D texture, Vector2 position, int width, int height,int flag)
            :base(texture,position,width,height,flag)
        { }
        public AutoTank(Texture2D texture, Vector2 position, int flag)
            :base(texture,position,Constant.TANK_SIZE_WIDTH,Constant.TANK_SIZE_HEIGHT,flag)
        { }
        #endregion

        #region Function

        public override bool Update()
        {
            count++;
            return base.Update();
        }
        public override bool TestMove(List<GameObject> Obstruction)
        {
            int temp = 0;
            while (!base.TestMove(Obstruction))
            {
                flag = random.Next(4) + 1;
                temp++;
                if (temp > 10)
                {
                    flag = preFlag;
                   // while (!base.TestMove(Obstruction)) MoveBack();
                    return true;// tránh vòng lặp vô hạn khi sinh ra lỗi chuyển hướng của tank tự động
                }
            }
            return true;
        }
        protected override void Intallization(Texture2D texture, Vector2 position, int width, int height)
        {
            base.Intallization(texture, position, width, height);
            random = new Random(DateTime.Now.Millisecond * 5);
            shootDelay = random.Next(Constant.TANK_SHOOT_DELAY, 2 * Constant.TANK_SHOOT_DELAY);
        }

        #endregion
    }
}
